package com.augurit.common.workdayholiday.model;


/**
 * 时间长度类，表示两个时刻相差多少小时多少分钟
 */
public class TimeSpan  implements Comparable<TimeSpan>{
	private int hours;		//多少个小时
	private int minutes;	//多少个分钟
	
	public TimeSpan(int hours, int minutes){
		this.hours = hours;
		this.minutes = minutes;
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	public int getMinutes() {
		return minutes;
	}
	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}
	
	/**
	 * 两个时长相加
	 * @param other
	 * @return
	 */
	public TimeSpan add(TimeSpan other){
		int forward = 0;
		if(this.minutes + other.minutes > 60){
			forward = 1;
		}
		return new TimeSpan(this.hours+other.hours+forward, this.minutes+other.minutes);
	}
	
	/**
	 * 两个时长相减，
	 * @param other 另一个时间段
	 * @return 时间段
	 */
	public TimeSpan substract(TimeSpan other){
		int forward = 0;
		int backward = 0;
		if(this.minutes < other.minutes){
			forward = 60;
			backward = 1;
		}
		return new TimeSpan(this.hours-other.hours-backward, this.minutes+forward-other.minutes);
	}
	
	/**
	 * 比较两个时长的顺序
	 * @param other
	 * @return 如果参数 other 等于此 TimeSpan，则返回值 0；如果此 TimeSpan 在 other 参数之前，则返回-1；如果此 TimeSpan 在 other 参数之后，则返回1。 
	 */
	public int compareTo(final TimeSpan other){
		if(this.hours > other.hours)return 1;
		if(this.hours == other.hours && this.minutes > other.minutes)return 1;
		if(this.hours == other.hours && this.minutes == other.minutes)return 0;
		return -1;
	}
	
	/**
	 * 计算时长
	 * @param startHour
	 * @param startMinute
	 * @param endHour
	 * @param endMinute
	 * @return
	 */
	public static TimeSpan calTimeSpan(int startHour, int startMinute, int endHour, int endMinute){
		if(endMinute < startMinute){
			endHour--;
			endMinute = endMinute + 60;
		}
		return new TimeSpan(endHour-startHour,endMinute - startMinute);
	}
	
	/**
	 * 计算时长
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static TimeSpan calTimeSpan(Time startTime, Time endTime){
		return calTimeSpan(startTime.getHour(), startTime.getMinute(), endTime.getHour(), endTime.getMinute());
	}
	
}
